package com.decent.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.decent.dao.ProductMapper;
import com.decent.entity.Product;
import com.decent.entity.common.MyException;
import com.decent.service.ProductService;
import com.decent.enums.MessageEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.List;

/**
 * @Author decent
 * @Create 2021/4/7 - 17:07
 * @Description
 */
@Service
public class ProductServiceImpl implements ProductService {

    private final static String PRICE_BOTTOM_TO_HIGH = "sell_price#ASC";
    private final static String PRICE_HIGH_TO_BOTTOM = "sell_price#DESC";
    private final static String DISCOUNT_BOTTOM_TO_HIGH = "discount#ASC";
    private final static String DISCOUNT_HIGH_TO_BOTTOM = "discount#DESC";


    @Resource
    private ProductMapper productMapper;

    /**
     * 获取热门产品
     *
     * @return List{id, productName, picUrl, sellPrice, officialPrice}
     */
    @Override
    public List<Product> listHotProduct() {
        return productMapper.selectList(Wrappers.<Product>query()
                .select("id","product_name", "pic_url", "sell_price", "official_price")
                .eq("is_hot","1").eq("sell_status", 1));
    }

    /**
     * 获取产品列表
     *
     * @param brandId    品牌ID
     * @param haveStock  仅看有货	默认值:true
     * @param sortType   排序种类
     *                   PRICE_BOTTOM_TO_HIGH	价格从底到高
     *                   PRICE_HIGH_TO_BOTTOM	价格从高到底
     *                   DISCOUNT_BOTTOM_TO_HIGH	折扣由底到高
     *                   DISCOUNT_HIGH_TO_BOTTOM	折扣由高到底
     * @param pageNumber 页码
     * @param pageSize   每页数量
     * @return id, productName, picUrl, sellPrice, officialPrice, discount, stock
     */
    @Override
    public PageInfo<Product> listProduct(Long brandId, Boolean haveStock,
                                         String sortType, Integer pageNumber, Integer pageSize) {

        Class<? extends ProductServiceImpl> aClass = ProductServiceImpl.class;
        Field field = null;
        String fieldValue = null;

        try {
            field = aClass.getDeclaredField(sortType);
            field.setAccessible(true);
            fieldValue = (String) field.get(aClass.newInstance());
        } catch (NoSuchFieldException e) {
            throw new MyException(MessageEnum.SORT_TYPE_PARAM_ERROR);
        } catch (IllegalAccessException
                | InstantiationException e) {
            e.printStackTrace();
        }
        assert fieldValue != null;
        String finalFieldValue = fieldValue;
        return PageHelper.startPage(pageNumber, pageSize).doSelectPageInfo(() -> {
            productMapper.selectList(Wrappers.<Product>query()
                    .select("id", "product_name", "pic_url", "sell_price", "official_price", "ROUND(sell_price*10/official_price, 1) AS discount", "stock")
                    .ge("stock",haveStock?1:0).eq("brand_id", brandId)
                    .orderBy(true,"ASC".equals(finalFieldValue.split("#")[1]), finalFieldValue.split("#")[0]));
        });
    }

    /**
     * 根据产品id 获取产品详情
     *
     * @param productId 产品id
     * @return product
     */
    @Override
    public Product productDetailsByProductId(Long productId) {
        return productMapper.selectOne(Wrappers.<Product>query()
                .select("id", "product_name","pic_url","sell_price","official_price",
                        "ROUND(sell_price*10/official_price, 1) AS discount","stock","buy_after_effective_time","exchange_after_effective_day","use_notice")
                .eq("id", productId));
    }

    /**
     * 根据产品id 获取产品信息
     *
     * @param productId 产品id
     * @return product
     */
    @Override
    public Product goodsNoByProductId(Long productId) {
        return productMapper.selectOne(Wrappers.<Product>query()
                .select("id", "product_name","pic_url","sell_price","official_price","goods_no",
                        "stock","buy_after_effective_time","exchange_after_effective_day")
                .eq("id", productId).eq("sell_status", 1));
    }


    /**
     * 根据产品id 修改库存
     *
     * @param productId 产品id
     * @param count 购买数量
     * @return 影响行数
     */
    @Override
    public Integer updateStockByProductId(Long productId, Integer count) {
        return productMapper.updateByPrimaryKey(productId, count);
    }
}
